<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>二手书交易平台 - 登录</title>
    <link href="https://fonts.googleapis.com/css?family=Roboto:400,700&display=swap" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%);
            font-family: 'Roboto', sans-serif;
            min-height: 100vh;
            margin: 0;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .login-container {
            background: #fff;
            padding: 40px 32px 32px 32px;
            border-radius: 16px;
            box-shadow: 0 8px 32px rgba(0,0,0,0.15);
            width: 350px;
            max-width: 90vw;
            text-align: center;
        }
        .login-title {
            font-size: 2rem;
            font-weight: 700;
            color: #2d3a4b;
            margin-bottom: 16px;
        }
        .login-subtitle {
            color: #6c7a89;
            font-size: 1rem;
            margin-bottom: 32px;
        }
        .login-form {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }
        .login-form input {
            padding: 12px 14px;
            border: 1px solid #d1d9e6;
            border-radius: 8px;
            font-size: 1rem;
            outline: none;
            transition: border 0.2s;
        }
        .login-form input:focus {
            border-color: #7eb6ff;
        }
        .login-btn {
            background: linear-gradient(90deg, #7eb6ff 0%, #5eaefd 100%);
            color: #fff;
            font-weight: 700;
            border: none;
            border-radius: 8px;
            padding: 12px;
            font-size: 1.1rem;
            cursor: pointer;
            transition: background 0.2s;
        }
        .login-btn:hover {
            background: linear-gradient(90deg, #5eaefd 0%, #7eb6ff 100%);
        }
        .login-footer {
            margin-top: 24px;
            color: #a0aab8;
            font-size: 0.95rem;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-title">二手书交易平台</div>
        <div class="login-subtitle">欢迎登录，开启你的书籍循环之旅</div>
        <form class="login-form" id="loginForm">
            <input type="text" id="username" name="username" placeholder="用户名" required>
            <input type="password" id="password" name="password" placeholder="密码" required>
            <button type="submit" class="login-btn">登录</button>
        </form>
        <div class="login-footer">还没有账号？<a href="register.html" style="color:#5eaefd;text-decoration:none;">立即注册</a></div>
    </div>
    <script>
        import("./api_config.js").then(({ API_BASE_URL }) => {
            document.getElementById('loginForm').addEventListener('submit', async function(e) {
                e.preventDefault();
                const username = document.getElementById('username').value;
                const password = document.getElementById('password').value;
                try {
                    const response = await fetch(`${API_BASE_URL}/userAccount/doLogin`, {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({ username, password })
                    });
                    const result = await response.json();
                    if (result.success) {
                        // 保存userId和userType到localStorage
                        if (result.data && typeof result.data.userType !== 'undefined') {
                            localStorage.setItem('userInfo', JSON.stringify({
                                userId: result.data.userId,
                                userType: result.data.userType
                            }));
                        }
                        if (result.data && result.data.userType === 1) {
                            window.location.href = 'merchant_home.html';
                        } else if (result.data && result.data.userType === 0) {
                            window.location.href = 'user_books.html';
                        } else {
                            alert('登录成功！');
                        }
                    } else {
                        alert(result.message || '登录失败');
                    }
                } catch (err) {
                    alert('网络错误或服务器无响应');
                }
            });
        });
        // 登录校验
        (function() {
            var userInfo = localStorage.getItem('userInfo');
            if (userInfo) {
                // ... existing code ...
            }
        })();
        // 注册按钮跳转
        var registerBtn = document.getElementById('registerBtn');
        if (registerBtn) {
            registerBtn.onclick = function() {
                window.location.href = 'register.html';
            };
        }
    </script>
</body>
</html> 